export const transferResponse = (res: any) => {
	if (res && res.options) {
		// 字典数据
		res.options.map((item: any) => {
			if (typeof item.code === 'string') {
				item.code = parseInt(item.code)
			}
			return item
		})
		return res.options
	} else if (res && res.rows) {
		res.rows = wrapperList(res.rows)
		return res
	} else {
		console.log('单条数据', res)
		return wrapperObject(res)
	}
}
export const transferCodeResponse = (res: any) => {
	console.log('请求客户端', res)

	if (res && res.code === '0') {
		if (res.data && res.data.options) {
			// console.log('字典数据', res.data)
			// 字典数据
			if (res.data.options) {
				res.data.options.map((item: any) => {
					if (typeof item.code === 'string') {
						item.code = parseInt(item.code)
					}
					return item
				})
			}
			return res.data.options
		} else if (res.data && res.data.rows) {
			//   console.log('列表数据', res.data)
			res.data.rows = wrapperList(res.data.rows)
			return res.data
		} else {
			//   console.log('单条数据', res)
			res.data = wrapperObject(res.data)
			return res.data
		}
	} else {
		console.log('请求错误:', res)
		// 错误自动弹窗 qt端目前无法进行异常处理 所以自己抛出异常
		// 重要的提交请求这里必须要有 否则错误代码也会继续执行

		throw Error(res.msg)
	}
}
/**
 *
 * @param {Array} list   原始数据列表
 * @param {int} currentPage   当前查询的页码
 * @param {boolean} wrapper   是否需要编码
 */
export const wrapperList = (list: any) => {
	const tbList: any = []
	if (!list || list.length === 0) return
	const wrapper = !!list[0].bean
	list.find((item: any) => {
		let obj: any
		let caption: any
		if (wrapper === true) {
			obj = item.bean
			caption = item.caption
		} else {
			obj = item
		}
		if (wrapper === true && caption) {
			Object.keys(caption).map((wItem) => {
				obj[wItem + 'Caption'] = caption[wItem]
			})
		}
		tbList.push(obj)
	})
	return tbList
}
/**
 *
 * @param {Object} data   原始数据
 * @param {boolean} wrapper   是否需要编码
 */
export const wrapperObject = (data: any) => {
	if (data === '' || data === null) return ''
	let obj: any
	let caption: any
	const wrapper = !!data.bean
	if (wrapper === true) {
		obj = data.bean
		caption = data.caption
	} else {
		obj = data
	}
	if (wrapper === true && caption) {
		Object.keys(caption).map((wItem) => {
			obj[wItem + 'Caption'] = caption[wItem]
		})
	}
	return obj
}

type OptionsItem = {
	label: string
	value: string
	disabled?: boolean
}
export function transferOptions(
	options: any[],
	labelField: string = 'label',
	valueField: string = 'value',
	numberToString: boolean = false
) {
	console.log('numberToString', numberToString)

	return options.reduce((prev, next: Recordable) => {
		if (next) {
			const value = next[valueField]
			prev.push({
				label: next[labelField],
				value: numberToString ? `${value}` : value,
			})
		}
		return prev
	}, [] as OptionsItem[])
}
